<template>
	<view class="page-container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="search-container flex-center linecamp-1" style="margin: 0 16rpx;" @click="jumpBack()">
					<image src="@/static/image/common/search.png" style="width: 32rpx; height: 32rpx"
						class="clear-flex u-m-r-14"></image>
					<view class="u-f-32 clear-flex linecamp-1" style="width: 480rpx;">{{ keyword }}</view>
				</view>
				<view class="clear-flex" @click="jumpBack()">{{ lang_common.cancel }}</view>
			</view>

			<view class="flex-center-between" style="padding: 0 12rpx 0 40rpx;">
				<view class="u-f-32 u-lh-44 font-medio">{{ lang_index.searchResult }}</view>
				<image src="@/static/image/common/filter_btn.png" style="height: 88rpx;width: 88rpx;" class="clear-flex"
					@click="filterVisible = true"></image>
			</view>
		</view>
		<!-- 列表 -->
		<view>
			<mescroll-body height="calc(100vh - 198rpx - var(--status-bar-height))" ref="mescrollRef"
				@init="mescrollInit" @down="downCallback" @up="upCallback">
				<view>
					<view v-for="(item, index) in dataList" :key="index" class="u-p-t-12" style="padding: 0 30rpx;">
						<shop-card instance :item="item" @phone="settingValue = 'contact'"></shop-card>
					</view>
				</view>
			</mescroll-body>
		</view>

		<!-- 筛选弹窗 -->
		<x-popup v-model="filterVisible" mode="bottom" zIndex="999999" @close="filterVisible = false">
			<view class="filter-container">
				<!-- 头部 -->
				<view class="flex-center-between u-m-b-36" style="position: relative;">
					<image src="@/static/image/common/close_grew.png" style="width: 30rpx; height: 28rpx;"
						class="clear-flex" @click="filterVisible = false"></image>

					<view class="u-f-36 u-lh-50 common-title font-medio">{{ lang_common.filter }}</view>

					<view class="u-f-32 u-lh-44 font-medio" @click="downCallback">{{ lang_common.done }}
					</view>
				</view>

				<!-- 分割线 -->
				<u-gap height="3" bgColor="#F9F9F9"></u-gap>

				<!-- 选项 -->
				<!-- <view class="u-m-t-22 flex-center">
					<view class="u-m-r-28 u-f-30 u-lh-42 font-medio color-grew clear-flex">{{ lang_index.price }}</view>
					<view class="flex-center-between" style="width: 100%;">
						<view v-for="(item, index) in filters" :key="index" :class="['filter-item u-text-center flex-center-center',
						filterIndex === index ? 'filter-item-active' : '']" @click="filterIndex = index">
							<view class="u-f-30 u-lh-42">{{ lang_index[item.key] }}</view>
							<view v-if="filterIndex === index" class="filter-item-icon u-f-20">
								<image src="@/static/image/common/select_on.png" class="filter-item-icon-content">
								</image>
							</view>
						</view>
					</view>
				</view> -->

				<view class="u-m-t-54 flex-center">
					<view class="u-m-r-28 u-f-30 u-lh-42 font-medio color-grew clear-flex">{{ lang_index.area }}</view>
					<view class="flex-center-between" style="width: 100%;">
						<view></view>
						<view class="flex-center" @click="getArea">
							<view class="u-m-r-8 u-f-30 u-lh-42 clear-flex">{{ areaFormat || lang_index.selectCity }}
							</view>
							<u-icon name="right" custom-prefix="u" size="19" color="#999"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</x-popup>
		
		<permission-unAuthTips v-model="settingValue"></permission-unAuthTips>
	</view>
</template>

<script>
	import {
		merchantPage
	} from '@/api/user/merchant.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				keyword: '',
				area: {},

				filterVisible: false,
				filterIndex: -1,
				filters: [],

				dataList: [],
				
				settingValue: ''
			}
		},
		computed: {
			areaFormat() {
				let str = '',
					area = this.area
				for (let key in area) {
					let item = area[key]
					item && item.title && (str = str + item.title + ',')
				}
				str && (str = str.slice(0, -1))
				return str
			}
		},
		// 获取类型
		onLoad({
			keyword
		}) {
			this.keyword = keyword
			// 初始化筛选选项
			let filters = [{
					key: 'priceUp',
					label: '价格升序'
				},
				{
					key: 'priceDown',
					label: '价格降序'
				}
			].map((item, index) => {
				let result = {
					...item,
					value: index
				}
				return result
			})
			this.filters = filters
		},
		methods: {
			getArea() {
				uni.navigateTo({
					url: '/pages/other/city/city',
					events: {
						change: res => {
							this.area = res
						}
					},
					success: res => {
						res.eventChannel.emit('city-page', this.area)
					}
				})
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef']
				let queryObj = {}
				if (this.area.province) {
					queryObj.province = this.area.province.title
					queryObj.city = this.area.city.title
				}
				merchantPage({
					pageNumber: page.num,
					pageSize: page.size,
					keyword: this.keyword,
					...queryObj
				}).then(res => {
					this.filterVisible = false
					node && node.mescroll.endSuccess(res.list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(res.list); //追加新数据
					this.filterVisible = false
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef']
				node && node.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-container {
		width: 100%;
		padding: 16rpx 30rpx;
		border-radius: 100rpx;
		background-color: #F6F6F6;
	}

	.filter {
		&-container {
			padding: 48rpx 36rpx calc(env(safe-area-inset-bottom) + 48rpx);
			border-top-left-radius: 40rpx;
			border-top-right-radius: 40rpx;
			background-color: #FFFFFF;
		}

		&-item {
			width: 272rpx;
			height: 72rpx;
			border-radius: 8rpx;
			background-color: rgba(245, 245, 245, 1);

			&-active {
				position: relative;
				color: #FC2642;
				background-color: rgba(252, 38, 66, 0.14);
				overflow: hidden;
			}

			&-icon {
				position: absolute;
				right: 0rpx;
				bottom: 0rpx;
				width: 40rpx;
				height: 32rpx;
				color: #FFFFFF;
				transform: rotate(135deg);
			}

			&-icon-content {
				position: absolute;
				top: 0;
				right: 0;
				width: 40rpx;
				height: 32rpx;
				transform: rotate(-135deg);
			}
		}
	}
</style>
